package com.sh.ssao.lottery.config.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.stereotype.Component;

@Component
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Result<T> {
    /** 错误码. */
    private Integer code;

    /** 提示信息. */
    private String msg;

    /** 具体的内容. */
    private T data;

    /**
     * 操作成功
     * @param data
     * @return
     */
    public Result<T> ok(T data){
        this.code = RCE.SUCCESS.getCode();
        this.msg = RCE.SUCCESS.getMessage();
        this.data = data;
        return this;
    }

    /**
     * 自定义返回提示消息
     * @param data
     * @param msg
     * @return
     */
    public Result<T> ok(T data,String msg){
        this.code = RCE.SUCCESS.getCode();
        this.msg = "".equals(msg)||null==msg?RCE.SUCCESS.getMessage():msg;
        this.data = data;
        return this;
    }

    /**
     * 操作失败
     * @param data
     * @return
     */
    public Result<T> fail(T data){
        this.code = RCE.ERROR.getCode();
        this.msg = RCE.ERROR.getMessage();
        this.data = data;
        return this;
    }

    /**
     * 通用可扩展返回结果
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public Result rt(Integer code,String msg,T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
        return this;
    }
}
